using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.Schedule;
namespace Janus.NorthwindDemo2007
{
	public partial class PageSetup
	{

		private System.Drawing.Font mHeaderFont;
		private System.Drawing.Font mFooterFont;
		private System.Drawing.Font mHeadingsFont;
		private System.Drawing.Font mAppointmentsFont;
		public Janus.Windows.Schedule.ScheduleView mCurrentView;
		public Janus.Windows.Schedule.SchedulePrintDocument schedulePrintDocument;
		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			NorthwindApp.MainForm.VisualStyleManager1.AddControl(this, true);
		}
		public Font HeaderFont
		{
			get
			{
				return mHeaderFont;
			}
			set
			{
				if (mHeaderFont == null || ! mHeaderFont.Equals(value))
				{
					mHeaderFont = value;
					this.ChangeEditBox(txtHeader, mHeaderFont);
				}
			}
		}

		public Font FooterFont
		{
			get
			{
				return mFooterFont;
			}
			set
			{
				if (mFooterFont == null || ! mFooterFont.Equals(value))
				{
					mFooterFont = value;
					this.ChangeEditBox(txtFooter, FooterFont);
				}
			}
		}

		public Font HeadingsFont
		{
			get
			{
				return mHeadingsFont;
			}
			set
			{
				if (mHeadingsFont == null || ! mHeadingsFont.Equals(value))
				{
					mHeadingsFont = value;
					this.ChangeEditBox(txtHeadingsFont, HeadingsFont);
				}
			}
		}

		public Font AppointmentsFont
		{
			get
			{
				return mAppointmentsFont;
			}
			set
			{
				if (mAppointmentsFont == null || ! mAppointmentsFont.Equals(value))
				{
					mAppointmentsFont = value;
					this.ChangeEditBox(txtAppointmentFont, AppointmentsFont);
				}
			}
		}

		private void ChangeEditBox(Janus.Windows.GridEX.EditControls.EditBox editBox, System.Drawing.Font font)
		{
			editBox.Text = font.Size.ToString() + " pt. " + font.FontFamily.Name;
			System.Drawing.Font newFont = new Font(font.FontFamily, (float)(editBox.Font.Size), font.Style);
			editBox.Font = newFont;
		}

		private void SetPreviewImage()
		{
			Image img = null;
			if (this.mCurrentView == Janus.Windows.Schedule.ScheduleView.MonthView)
			{

                if ((LayoutPages)this.cboLayout.SelectedValue == LayoutPages.OnePage)
				{
					img = imgPreview.Images[4];
				}
				else
				{
					img = imgPreview.Images[5];
				}
			}
			else if (this.mCurrentView == Janus.Windows.Schedule.ScheduleView.WeekView)
			{

                if ((Janus.Windows.Schedule.LayoutPages)this.cboLayout.SelectedValue == LayoutPages.OnePage)
				{
					img = imgPreview.Images[2];
				}
				else
				{
					img = imgPreview.Images[3];
				}
			}
			else
			{
				if (this.schedulePrintDocument.Schedule.Dates.Count == 1)
				{
					img = imgPreview.Images[0];
				}
				else
				{
					img = imgPreview.Images[1];
				}
			}

			this.picPreview.Image = img;
		}

		private void textBox_Enter(object sender, System.EventArgs e)
		{

			ToolBar.Enabled = true;
		}

		private void textBox_Leave(object sender, System.EventArgs e)
		{

			ToolBar.Enabled = false;
		}

		private void btnHeader_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.Font = this.HeaderFont;
			if (this.fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				this.HeaderFont = this.fontDialog1.Font;
			}
		}

		private void btnFooter_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.Font = this.FooterFont;
			if (this.fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				this.FooterFont = this.fontDialog1.Font;
			}
		}

		private void btnHeading_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.Font = this.HeadingsFont;
			if (this.fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				this.HeadingsFont = this.fontDialog1.Font;
			}
		}

		private void btnAppointments_Click(object sender, System.EventArgs e)
		{
			this.fontDialog1.Font = this.AppointmentsFont;
			if (this.fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				this.AppointmentsFont = this.fontDialog1.Font;
			}
		}

		private void PageSetup_Load(object sender, System.EventArgs e)
		{

			this.mCurrentView = schedulePrintDocument.Schedule.View;

			if (mCurrentView == Janus.Windows.Schedule.ScheduleView.DayView || mCurrentView == Janus.Windows.Schedule.ScheduleView.WorkWeek)
			{
				this.cboLayout.Items.Add("One Page/Day", Janus.Windows.Schedule.DayViewPrintMode.PagePerDay);
				this.cboLayout.Items.Add("One Page/View", Janus.Windows.Schedule.DayViewPrintMode.PagePerDay);
				this.cboLayout.SelectedValue = schedulePrintDocument.DayViewPrintMode;
			}
			else
			{
				this.cboLayout.Items.Add("One Page", LayoutPages.OnePage);
				this.cboLayout.Items.Add("Two Pages", LayoutPages.TwoPages);
				this.cboLayout.SelectedValue = schedulePrintDocument.LayoutPages;
			}

			this.HeaderFont = schedulePrintDocument.HeaderFont;
			this.FooterFont = schedulePrintDocument.FooterFont;
			this.HeadingsFont = schedulePrintDocument.DateHeadingsFont;
			switch (mCurrentView)
			{
				case Janus.Windows.Schedule.ScheduleView.MonthView:
					chkCompress.Visible = true;
					chkCompress.Checked = schedulePrintDocument.CompressWeekEnds;
					AppointmentsFont = schedulePrintDocument.MonthFont;
					break;
				case Janus.Windows.Schedule.ScheduleView.WeekView:
					AppointmentsFont = schedulePrintDocument.WeekFont;
					break;
				default:
					AppointmentsFont = schedulePrintDocument.DayFont;
					lblStartTime.Enabled = true;
					lblEndTime.Enabled = true;
					this.clcStartTime.Enabled = true;
					this.clcEndTime.Enabled = true;
					this.clcStartTime.Value = DateTime.Today.Date.Add(schedulePrintDocument.StartTime);
					this.clcEndTime.Value = DateTime.Today.Date.Add(schedulePrintDocument.EndTime);
					break;
			}

			this.SetPreviewImage();
			lblCurrentView.Text += mCurrentView.ToString();
			txtLeftHeader.Text = schedulePrintDocument.HeaderLeft;
			txtCenterHeader.Text = schedulePrintDocument.HeaderCenter;
			txtRightHeader.Text = schedulePrintDocument.HeaderRight;
			txtLeftFooter.Text = schedulePrintDocument.FooterLeft;
			txtCenterFooter.Text = schedulePrintDocument.FooterCenter;
			txtRightFooter.Text = schedulePrintDocument.FooterRight;
			this.chkShading.Checked = schedulePrintDocument.GrayShading;
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			schedulePrintDocument.HeaderFont = this.HeaderFont;
			schedulePrintDocument.FooterFont = this.FooterFont;
			schedulePrintDocument.DateHeadingsFont = this.HeadingsFont;
			switch (mCurrentView)
			{
				case Janus.Windows.Schedule.ScheduleView.MonthView:
					schedulePrintDocument.CompressWeekEnds = chkCompress.Checked;
					schedulePrintDocument.MonthFont = AppointmentsFont;
					schedulePrintDocument.LayoutPages = (LayoutPages)this.cboLayout.SelectedValue;
					break;
				case Janus.Windows.Schedule.ScheduleView.WeekView:
					schedulePrintDocument.WeekFont = AppointmentsFont;
                    schedulePrintDocument.LayoutPages = (LayoutPages)this.cboLayout.SelectedValue;
					break;
				default:
					schedulePrintDocument.DayFont = AppointmentsFont;
					this.schedulePrintDocument.StartTime = this.clcStartTime.Value.TimeOfDay;
					this.schedulePrintDocument.EndTime = this.clcEndTime.Value.TimeOfDay;
                    schedulePrintDocument.DayViewPrintMode = (DayViewPrintMode)this.cboLayout.SelectedValue;
					break;
			}


			schedulePrintDocument.HeaderLeft = txtLeftHeader.Text;
			schedulePrintDocument.HeaderCenter = txtCenterHeader.Text;
			schedulePrintDocument.HeaderRight = txtRightHeader.Text;
			schedulePrintDocument.FooterLeft = txtLeftFooter.Text;
			schedulePrintDocument.FooterCenter = txtCenterFooter.Text;
			schedulePrintDocument.FooterRight = txtRightFooter.Text;
			schedulePrintDocument.GrayShading = this.chkShading.Checked;
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void cboLayout_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.SetPreviewImage();
		}

		private void btnPaper_Click(object sender, System.EventArgs e)
		{
			pageSetupDialog1.Document = this.schedulePrintDocument;
			this.pageSetupDialog1.ShowDialog();
		}

		private void UiCommandManager1_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
		{
			Janus.Windows.GridEX.EditControls.EditBox activeTextBox = null;
			if (this.ActiveControl != null && this.ActiveControl is Janus.Windows.GridEX.EditControls.EditBox)
			{
				activeTextBox = (Janus.Windows.GridEX.EditControls.EditBox)this.ActiveControl;
			}
			if (activeTextBox != null)
			{
				switch (e.Command.Key)
				{
					case "cmdPageNumber":
						activeTextBox.Text += "[Page #]";
						break;
					case "cmdTotalPages":
						activeTextBox.Text += "[Total Pages]";
						break;
					case "cmdDatePrinted":
						activeTextBox.Text += "[Date Printed]";
						break;
					case "cmdTimePrinted":
						activeTextBox.Text += "[Time Printed]";
						break;
					case "cmdUser":
						activeTextBox.Text += "[User Name]";
						break;
				}
			}
		}

	}

} //end of root namespace